﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class AssetBundleTools: AssetPostprocessor
{
    static void OnPostprocessAllAssets(string[] importedAssets,string[] deletedAssets,string[] movedAssets,string[] movedFromAssetPaths)
    {
        AutoSetAssetBundleName(importedAssets);
    }
    static string[] ResSuffix = { ".png", ".jpg" };
    static string RootDir = "Assets";
    static string lastName = ".assetbundle";

    //lua相关设置
    static string[] LuaResSuffix = { ".txt" };
    static string luaBundlleName = "luascript";

    /// <summary>
    /// 资源导入时自动设置AssetBundle name
    /// </summary>
    /// <param name="importedAssets"></param>
    static void AutoSetAssetBundleName(string[] importedAssets)
    {
        foreach (var path in importedAssets)
        {
            //是否GameContent目录下
            int firstIndex = path.IndexOf(RootDir) + RootDir.Length + 1;
            if (firstIndex == -1)
                continue;

            //后缀是否对
            int secondIndex = -1;
            string pathLower = path.ToLower();
            for (int i = 0; i < ResSuffix.Length; i++)
            {
                if (pathLower.EndsWith(ResSuffix[i]))
                {
                    secondIndex = pathLower.LastIndexOf(ResSuffix[i]);
                    break;
                }
            }
            int luaIndex = -1;
            if (secondIndex == -1)
            {
                if (pathLower.EndsWith(LuaResSuffix[0]))
                {
                    luaIndex = pathLower.LastIndexOf(LuaResSuffix[0]);
                }
            }
            if(secondIndex == -1 && luaIndex == -1)
                continue;
            if (luaIndex != -1 && secondIndex == -1)
            {
                //是lua文件
                AutoSetAssetBundleNameByLua(luaIndex, firstIndex, pathLower, path);
            }
            else
            {
                secondIndex = pathLower.LastIndexOf("/");
                string bundleName = string.Empty;
                if (secondIndex >= 0)
                    bundleName = pathLower.Substring(firstIndex, secondIndex - firstIndex);

                //设置name
                AssetImporter importer = AssetImporter.GetAtPath(path);
                if (importer != null)
                {
                    importer.assetBundleName = bundleName + lastName;
                    importer.assetBundleVariant = "";
                }
            }
        }
    }


    static void AutoSetAssetBundleNameByLua(int luaIndex,int firstIndex, string pathLower,string path)
    {
        luaIndex = pathLower.LastIndexOf("/");
        string bundleName = string.Empty;
        if (luaIndex >= 0)
            bundleName = pathLower.Substring(firstIndex, luaIndex - firstIndex);

        //设置name
        AssetImporter importer = AssetImporter.GetAtPath(path);
        if (importer != null)
        {
            //importer.assetBundleName = bundleName + lastName;
            importer.assetBundleName = luaBundlleName + lastName;
            importer.assetBundleVariant = "";

        }
    }
}
